package com.timesheet.gui.tasktracker.composites;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.timesheet.gui.tasktracker.providers.TaskTrackerContentProvider;
import com.timesheet.gui.tasktracker.providers.TaskTrackerLabelProvider;
import com.timesheet.gui.tasktracker.viewmodel.TaskTrackerViewModel;
import com.timesheet.gui.tasktracker.viewmodel.TaskTrackerViewModelContainer;

import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class TaskTrackerSaveResultComposite extends Composite implements PropertyChangeListener {

	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());

	private Table table;

	FontRegistry fontRegistry = new FontRegistry(Display.getDefault());

	TaskTrackerViewModelContainer container = TaskTrackerViewModelContainer.getInstance();

	private TableViewer tableViewer;

	private Font font;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public TaskTrackerSaveResultComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));

		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		formToolkit.paintBordersFor(table);

		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnTaskno = tableViewerColumn.getColumn();
		tblclmnTaskno.setWidth(100);
		tblclmnTaskno.setText("TaskNo");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnTasktype = tableViewerColumn_1.getColumn();
		tblclmnTasktype.setWidth(100);
		tblclmnTasktype.setText("Tasktype");

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnStatus = tableViewerColumn_2.getColumn();
		tblclmnStatus.setWidth(100);
		tblclmnStatus.setText("Status");

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnStartDate = tableViewerColumn_3.getColumn();
		tblclmnStartDate.setWidth(100);
		tblclmnStartDate.setText("Start Date");

		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnEndDate = tableViewerColumn_4.getColumn();
		tblclmnEndDate.setWidth(100);
		tblclmnEndDate.setText("End date");

		tableViewer.setLabelProvider(new TaskTrackerLabelProvider());
		tableViewer.setContentProvider(new TaskTrackerContentProvider());
		TaskTrackerViewModel model = new TaskTrackerViewModel();
		model.setTaskType("hello");
		container.add(model);

		//List<TaskTrackerViewModel> list = container.getList();
		//container.setList(list);

		tableViewer.setInput(container);

		tableViewer.getTable().setFont(fontRegistry.get("code"));

		container.addPropertyChangeListener(this);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		Object oldValue = event.getOldValue();
		Object newValue = event.getNewValue();

		System.out.println("oldValue: " + oldValue);
		System.out.println("newValue: " + newValue);

		System.out.println(event.getPropertyName());

		//tableViewer.setInput(container);
		tableViewer.refresh();
		TaskTrackerViewModelContainer container = (TaskTrackerViewModelContainer) tableViewer.getInput();
		System.out.println(container.getList());

	}

	public TableViewer getTableViewer() {
		return tableViewer;
	}

}
